limsResult.runOrderTaskAuditDetailPage = {
    // 哪些分析项是富文本外链编辑
    richLinkAnalyte: null,

    getBtnToolbar: function (runId) {
        var _this = this;
        var items = [{
            type: 'button',
            text: 'GIKAM.BUTTON.BACK',
            icon: 'back',
            onClick: function () {
                if (_this.param.modal) {
                    Gikam.getLastModal().close()
                } else {
                    workspace.window.goBack()
                }
            }
        }, {
            type: 'button',
            text: 'GIKAM.BPMN.COMMENT.PASS',
            icon: 'submit', color: 'success',
            onClick: function () {
                _this.submitRunAuditSingle(runId)
            }
        }, {
            type: 'button',
            text: 'GIKAM.BPMN.COMMENT.REJECT',
            icon: 'reject',
            onClick: function () {
                _this.reviewRejectSingle(runId);
            }
        },]
        return {
            type: 'btnToolbar',
            items: items
        }
    },

    // 提交复核
    submitRunAuditSingle: function (runId) {
        // let grid = Gikam.getComp('run-audit-list-grid')
        var _this = this
        if (!runId) {
            return
        }
        Gikam.confirm('GIKAM.MODAL.SUBMIT', 'GIKAM.TIP.SUBMIT', function () {
            workspace.window.showMask();
            Gikam.postText(limsResult.runUrl + '/action/pass',
                Gikam.getJsonWrapper(null, [null, [{id: runId}]])).done(function (result) {
                // grid.refresh();
            }).always(function () {
                workspace.window.closeMask();
                workspace.window.goBack()
            });
        })
    },

    reviewRejectSingle: function (runId) {
        // let grid = Gikam.getComp('run-audit-list-grid');
        if (!runId) {
            return
        }
        Gikam.createLimsModal({
            title: 'MODULE.BUSINESS.RESULT_AUDIT.BUTTON.RETURNBACK',
            height: 220,
            width: 380,
            onConfirm: function () {
                var form = Gikam.getComp('confirm-form');
                if (form.validate()) {
                    let data = form.getData();
                    workspace.window.showMask();
                    Gikam.postText(limsResult.runUrl + '/action/reject',
                        Gikam.getJsonWrapper(data, [null, [{id:runId}]])).done(function (result) {
                    }).always(function () {
                        Gikam.getLastModal().close()
                        workspace.window.closeMask()
                        workspace.window.goBack()
                    });
                }
            },
            m: {
                type: 'form',
                id: 'confirm-form',
                fields: [{
                    field: 'reason',
                    title: 'T_LIMS_RUN.REVIEWREJECTREASON',
                    validators: ['notEmpty'],
                    type: 'textarea',
                    height: 100,
                }],
                columns: 1,
            }
        })
    },

    refreshGridColumns: function (grid, runId) {
        var _this = this;
        var url = Gikam.printf(limsResult.baseUrl + '/r/{runId}/result-to-columns/queries', {
            runId: runId
        });
        limsResult.analyteColumns = limsResult.getRunOrderTaskGridColumns();
        Gikam.postSync(url).done(function (data) {
            if (Gikam.isNotEmpty(data)) {
                // 富文本外链分析项记录到richLinkAnalyte键值对中
                _this.richLinkAnalyte = new Map();
                Gikam.each(data.rows, function (index, item) {
                    var analyteId = item.analyteId;
                    var fieldType = item.analType === 'N' ? 'number' : 'text';
                    var selectData = [];
                    if (item.ext$.possibleresultinfo) {
                        fieldType = 'select';
                        Gikam.each(JSON.parse(item.ext$.possibleresultinfo), function (index, item2) {
                            selectData.push({
                                text: item2.originalResult,
                                value: item2.originalResult
                            });
                        });
                    }
                    if (item.analType === "F") {
                        // 2023-02-27 迁移寻因
                        if (item.writeFlag === '1') {
                            limsResult.analyteColumns.push({
                                field: 'ext$.' + analyteId,
                                title: item.analyte,
                                type: 'link',
                                validators: ['notEmpty'],
                                items: selectData,
                                width: 180,
                                sort: false,
                                // editor: editor,
                                autoSave: false,
                            });
                        } else {
                            limsResult.analyteColumns.push({
                                field: 'ext$.' + analyteId,
                                title: item.analyte,
                                type: 'link',
                                items: selectData,
                                width: 180,
                                sort: false,
                                // editor: editor,
                                autoSave: false,
                            });
                        }
                        // 键为field:ext$.analyteId，值为t_lims_result表的Id
                        _this.richLinkAnalyte.set('ext$.' + analyteId, item.id);
                    } else if (item.writeFlag === '1') {
                        limsResult.analyteColumns.push({
                            field: 'ext$.' + analyteId,
                            title: item.analyte,
                            type: fieldType,
                            validators: ['notEmpty'],
                            items: selectData,
                            width: 180,
                            sort: false,
                            autoSave: false,
                            onBeforeEditorRender: function (row) {
                                return row.ext$.resultinfo && eval('(' + row.ext$.resultinfo + ')').hasOwnProperty(analyteId);
                            },
                            styleFormatter: function (row) {
                                return {
                                    backgroundColor: row.ext$.resultinfo ? eval('(' + row.ext$.resultinfo + ')')[analyteId] : null
                                }
                            }
                        });
                    } else {
                        limsResult.analyteColumns.push({
                            field: 'ext$.' + analyteId,
                            title: item.analyte,
                            type: fieldType,
                            items: selectData,
                            width: 180,
                            sort: false,
                            onBeforeEditorRender: function (row) {
                                return row.ext$.resultinfo && eval('(' + row.ext$.resultinfo + ')').hasOwnProperty(analyteId);
                            },
                            styleFormatter: function (row) {
                                return {
                                    backgroundColor: row.ext$.resultinfo ? eval('(' + row.ext$.resultinfo + ')')[analyteId] : null
                                }
                            }
                        });
                    }
                });
            }
        });
        grid.cleanData();
        grid.options.columns = limsResult.analyteColumns;
        grid.refreshColumns(limsResult.analyteColumns);
    },

    getRunOrderTaskTab: function (runId) {
        var _this = this;
        return {
            type: 'tab',
            panels: [{
                title: 'T_LIMS_ORDER_TASK.TITLE.SAMPLE',
                items: [{
                    type: 'grid',
                    height: 230,
                    id: 'result-run-order-task-with-analyte-audit-grid',
                    page: false,
                    columns: limsResult.getRunOrderTaskGridColumns(),
                    generalButtonGroup: ['refresh', 'config'],
                    url: limsResult.orderTaskUrl + '/result/queries',
                    requestData: {
                        status_EQ: 'approve',
                        runId_EQ: runId,
                        flag_EQ: '1'
                    },
                    onRendered: function () {
                        var grid = Gikam.getComp('result-run-order-task-with-analyte-audit-grid')
                        _this.refreshGridColumns(grid, runId)
                    },
                    onCellClick: function (field, row) {
                        // 分析项ext$.analyteId，字段为t_lims_analyte的id,详情必须为t_lims_result的id，判断富文本外链分析项
                        _this.richLinkAnalyte.has(field) && limsResult.loadRichText(row.id, field);
                    },
                }]
            }, {
                title: 'MODULE.BUSINESS.RESULT.TAB.DERIVATIVE',
                items: [{
                    type: 'grid',
                    id: 'result-all-derivative-audit-grid',
                    columns: limsResult.getAllDerivativeEditListColumns(),
                    url: Gikam.printf(limsResult.derivativelUrl + '/runs/{runId}/queries', {
                        runId: runId
                    }),
                }]
            }, {
                title : 'T_LIMS_ORDER_TASK.TITLE.BOARD', // 布板信息
                items : [{
                    type : 'iframe',
                    src : '/static/basemodule/business/run-board-readonly/index.html',
                    id : 'ksh-run-board-readonly-iframe1'
                }]
            },{
                title: 'GIKAM.FILE.ATTACHMENT',
                items: [{
                    type: 'uploader',
                    id: 'result-audit-uploader',
                    filter: false,
                    readonly: true,
                    dbTable: 'T_LIMS_RUN',
                    bizId: _this.param.runCode,
                    formatterGridColumns: function (columns) {
                        columns.splice(2, 0, {
                            field: 'bizCategory',
                            title: 'LIMS.MODLE.FILE.FILETYPE',
                            type: 'select',
                            width: 140,
                            category: 'sys',
                            param: {
                                codeCategoryId: 'fjType'
                            }
                        });
                        return Gikam.getFormatterGridColumns(columns, true);
                    }
                }]
            }, {
                title: 'MODULE.COREDATA.TEST.TAB.MATERIAL_ITEM',
                items: [{
                    type: 'grid',
                    id: 'result-all-material-audit-grid',
                    service: 'limsRunMaterialServiceImpl',
                    readonly: true,
                    columns: limsResult.getMaterialListColumns(),
                    url: limsResult.runMaterialUrl + '/queries',
                    requestData: {
                        runId_EQ: runId
                    }
                }]
            }, {
                title: 'MODULE.COREDATA.TEST.TAB.EQUIP_ITEM',
                items: [{
                    type: 'grid',
                    id: 'result-all-equipt-audit-grid',
                    columns: limsResult.getEquiptListColumns(),
                    url: limsResult.runEquipUrl + '/queries',
                    requestData: {
                        runId_EQ: runId
                    }
                }]
            }],
            onActived: function (runId) {
                if (_this.param.id){
                    Gikam.postSync(limsResult.boardHoleUrl + '/queries', Gikam.getJsonWrapper({
                        f: {
                            boardId_EQ: _this.param.id
                        },
                        o: [{
                            y : 'asc'
                        },{
                            x : 'asc'
                        }],
                        n: -1, s: -1
                    })).done(function (data) {
                        const  dom=document.querySelector("#ksh-run-board-readonly-iframe1 >iframe");
                        let flag=false;  //是否初次加载
                        dom.onload=function(){
                            flag =true;
                            dom.contentWindow.postMessage({
                                type:1,
                                data: data.rows
                            }, "*");
                        };
                        if(!flag){
                            dom.contentWindow.postMessage({
                                type:1,
                                data: data.rows
                            }, "*");
                        }
                    });
                }else{
                    const  dom=document.querySelector("#ksh-run-board-readonly-iframe1 >iframe");
                    if (Gikam.isEmpty(dom)) return;
                    let flag=false;  //是否初次加载
                    dom.onload=function(){
                        flag =true;
                        dom.contentWindow.postMessage({
                            type:1,
                            data: []
                        }, "*");
                    };
                    if(!flag){
                        dom.contentWindow.postMessage({
                            type:1,
                            data: []
                        }, "*");
                    }
                }
            }
        }
    },

    createPage: function () {
        var _this = this;
        var render = workspace.window.$dom;
        if (_this.param.modal) {
            render = Gikam.getLastModal().window.$dom;
        }
        Gikam.create('layout', {
            center: {
                items: [this.getBtnToolbar(_this.param.id), _this.getRunOrderTaskTab(_this.param.id)]
            },
            renderTo: render
        });
        return this;
    },

    init: function (param) {
        this.param = param
        this.createPage()
    }
}
